﻿using JmShared;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace WebSocketServer
{
    public static class Extensions
    {
        public static string TrimEnd(this string source, string value)
        {
            if (!source.EndsWith(value))
                return source;

            return source.Remove(source.LastIndexOf(value, StringComparison.Ordinal));
        }

        public static string GetMD5(this byte[] array)
        {
            using (var md5 = MD5.Create())
            {
                return BitConverter.ToString(md5.ComputeHash(array)).Replace("-", string.Empty).ToLower();
            }
        }

        public static string GetMD5(this FileInfo file)
        {
            using (var md5 = MD5.Create())
            {
                using (Stream stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
                }
            }
        }

        public static bool IsMobileClient(this DeviceType devicetype)
        {
            return devicetype == DeviceType.Android || devicetype == DeviceType.WindowsPhone;
        }
    }
}
